package com.mmd.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
    static SimpleDateFormat standardFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    public final static String FmtOfDate = "yyyy-MM-dd";

    public final static String FmtOfDateTime = "yyyy-MM-dd HH:mm:ss";
    public final static String yyyyMMddHHmmss = "yyyyMMddHHmmss";

    public final static String FmtOfDateTime2 = "yyyy/MM/dd HH:mm:ss";

    public final static String FmtOfDate2 = "yyyy/MM/dd";

    public final static String FmtOfMonthAndDay = "MM月dd号";


    /**
     * 获取当前时间的毫秒值
     */
    public static long getNowTime() throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowtime = df.format(new Date());
        long millionSeconds = df.parse(nowtime).getTime();
        return millionSeconds;
    }

    /**
     * 将毫秒值转为日期格式
     *
     * @param ms
     * @return
     */
    public static String msToDate(long ms) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(ms);
        String time = formatter.format(calendar.getTime());
        return time;
    }

    /**
     * 将日期格式转为毫秒值
     *
     * @throws ParseException
     */
    public static long dateToMs(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long millionSeconds = 0;
        try {
            millionSeconds = sdf.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }//毫秒
        return millionSeconds;
    }

    /**
     * 获取某个时间的几天后或几天前的时间
     *
     * @param dateMs 传入的时间的毫秒值
     * @param day    几天后或几天前
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String getDayAfterTime(long dateMs, int day) {
        Calendar c = new GregorianCalendar();
        c.setTimeInMillis(dateMs);
        c.add(Calendar.DAY_OF_MONTH, day);//day天后
        return DateUtil.msToDate(c.getTimeInMillis());
    }

    /**
     * 获取指定月初
     *
     * @param date
     * @return
     */
    public static String getMonthBegin(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        String time = getAppointTime(cal.getTime(), 0, 0, 0);
        return time;
    }

    /**
     * 获取指定月末
     *
     * @param date
     * @return
     */
    public static String getMonthEnd(Date date) {
        if (date == null) {
            date = new Date();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String time = getAppointTime(cal.getTime(), 23, 59, 59);
        return time;
    }

    /**
     * 获取指定时间
     *
     * @param date
     * @param hour
     * @param minute
     * @param million
     * @return
     */
    public static String getAppointTime(Date date, int hour, int minute,
                                        int million) {
        if (date == null) {
            date = new Date();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // cal.set(Calendar.DAY_OF_MONTH,1);
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, million);
        return date2String(cal.getTime());
    }

    /**
     * 把日期类型格式化成字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String date2String(Date date) {
        return date2String(date, null);
    }

    public static String date2String(Date date, String format) {
        date = date == null ? new Date() : date;
        SimpleDateFormat sdf = format == null ? standardFormat
                : new SimpleDateFormat(format);
        try {
            return sdf.format(date);
        } catch (Exception e) {
            return null;
        }
    }

    public static Date parseDateToDay(String str) {
        return parseDate(str, DateUtil.FmtOfDate);
    }

    public static Date parseDate(String source) {
        if (org.apache.commons.lang.StringUtils.isNotBlank(source)) {
            Date date = DateUtil.parseDateToTime(source);
            if (date == null) {
                try {
                    date = DateUtil.timestampToDate(source);
                } catch (Exception e) {

                }
            }
            if (date == null) {
                date = DateUtil.parseDateToDay(source);
            }
            if (date == null) {
                date = DateUtil.parseDate(source, DateUtil.FmtOfDateTime2);
            }
            if (date == null) {
                date = DateUtil.parseDate(source, DateUtil.FmtOfDate2);
            }
            return date;
        } else {
            return null;
        }
    }

    public static Date parseDateToTime(String str) {
        return parseDate(str, DateUtil.FmtOfDateTime);
    }

    public static Date parseDate(String str, String fmtStr) {
        SimpleDateFormat format = new SimpleDateFormat(fmtStr);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            //e.printStackTrace();
        }
        return date;
    }

    public static Date timestampToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Long time = new Long(str);
        String d = format.format(time);
        try {
            Date date = format.parse(d);
            return date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取某一天的开始时间
     *
     * @param date
     * @return
     */
    public static Date getStartTime(Date date) {
        Calendar thisDate = Calendar.getInstance();
        thisDate.setTime(date);
        thisDate.set(Calendar.HOUR_OF_DAY, 0);
        thisDate.set(Calendar.MINUTE, 0);
        thisDate.set(Calendar.SECOND, 0);
        thisDate.set(Calendar.MILLISECOND, 0);
        return thisDate.getTime();
    }

    /**
     * 获取某一天的结束时间
     *
     * @param date
     * @return
     */
    public static Date getEndTime(Date date) {
        Calendar thisDate = Calendar.getInstance();
        thisDate.setTime(date);
        thisDate.set(Calendar.HOUR_OF_DAY, 23);
        thisDate.set(Calendar.MINUTE, 59);
        thisDate.set(Calendar.SECOND, 59);
        thisDate.set(Calendar.MILLISECOND, 999);
        return thisDate.getTime();
    }
}
